package com.qiu.wbpostman.action;

import java.util.Map;

import weibo4j.User;
import weibo4j.http.AccessToken;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.qiu.wbpostman.service.WbService;

public class HelloAction implements Action {
	private User user;

	public String execute() {
		try {
			// 取得accessToken
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			AccessToken accessToken = (AccessToken) session.get("accessToken");
			// 将accessToken中的信息保存到持久化对象UserToken中
			WbService wbs = new WbService();
			Long id = Long.parseLong(accessToken.getUserId() + "");
			if (wbs.checkUserMailIsExist(id)) {
                //存在该用户且已设置邮箱
			} else {
				wbs.saveAUserToken(accessToken);
			}
			// 取得当前登录用户
			user = wbs.getCurrentUser(accessToken);
			// 将当前登录用户放入session中
			session.put("user", user);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
